package com.ibeeking.found.org.b.rest.service.impl;

import com.ibeeking.found.common.entity.AreaDTO;
import com.ibeeking.found.common.enums.DefaultEnum;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.UserUtils;

import com.ibeeking.gmc.global.api.feign.GlobalDictFeignClient;
import com.ibeeking.found.org.b.rest.service.IEmployeeAddressService;
import com.ibeeking.found.org.service.common.bo.EmployeeAddressBO;
import com.ibeeking.found.org.service.common.dos.EmployeeAddressDO;
import com.ibeeking.found.org.service.common.param.EmployeeAddressParam;
import com.ibeeking.found.org.service.common.vo.AreaVO;
import com.ibeeking.found.org.service.common.vo.EmployeeAddressVO;
import com.ibeeking.found.org.service.mapper.EmployeeAddressMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @ClassName EmployeeAddressServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class EmployeeAddressServiceImpl extends ServiceImpl<EmployeeAddressMapper, EmployeeAddressDO> implements IEmployeeAddressService {

    @Resource
    private EmployeeAddressMapper employeeAddressMapper;

    @Resource
    private GlobalDictFeignClient globalDictFeignClient;

    @Override
    public EmployeeAddressBO selById(Long id) {
        if (id == null) {
            return null;
        }
        EmployeeAddressDO entityDO = employeeAddressMapper.selectById(id);
        return BeanUtil.convertBean(entityDO, EmployeeAddressBO.class);
    }

    @Override
    public List<EmployeeAddressDO> selByEmployeeId(Long employeeId) {
        return employeeAddressMapper.selectList(new LambdaQueryWrapper<EmployeeAddressDO>().eq(EmployeeAddressDO::getEmployeeId, employeeId));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(EmployeeAddressParam param) {
        Long employeeId = param.getEmployeeId() == null ? UserUtils.getUserId() : param.getEmployeeId();
        EmployeeAddressDO entityDO = BeanUtil.convertBean(param, EmployeeAddressDO.class);
        if (DefaultEnum.YES.getValue().equals(entityDO.getHasDefault())) {
            //当前添加项设为默认时,先把老的默认改为非默认
            getBean().modifyOldDefaultYesToNo(employeeId);
        }
        entityDO.setEmployeeId(employeeId);
        entityDO.setCreateBy(employeeId);
        entityDO.setCreateTime(LocalDateTime.now());
        entityDO.setModifyTime(LocalDateTime.now());
        employeeAddressMapper.insert(entityDO);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(EmployeeAddressParam param) {
        Long employeeId = param.getEmployeeId() == null ? UserUtils.getUserId() : param.getEmployeeId();
        EmployeeAddressDO entityDO = BeanUtil.convertBean(param, EmployeeAddressDO.class);
        if (DefaultEnum.YES.getValue().equals(entityDO.getHasDefault())) {
            //当前添加项设为默认时,先把老的默认改为非默认
            getBean().modifyOldDefaultYesToNo(employeeId);
        }
        entityDO.setEmployeeId(employeeId);
        entityDO.setModifyBy(employeeId);
        entityDO.setModifyTime(LocalDateTime.now());
        if (param.getPhone().contains("*")) {
            param.setPhone(null);
        }
        employeeAddressMapper.updateById(entityDO);

        return true;
    }

    @Override
    public EmployeeAddressVO view(Long id) {
        EmployeeAddressBO entityBO = getBean().selById(id);
        if (entityBO == null) {
            return null;
        }
        EmployeeAddressVO entityVO = BeanUtil.convertBean(entityBO, EmployeeAddressVO.class);
        entityVO.setHidePhone(DataUtils.hidePhone(entityVO.getPhone()));
        //所在地区
        List<String> codeList = new ArrayList<>();
        codeList.add(entityVO.getProvinceCode());
        codeList.add(entityVO.getCityCode());
        codeList.add(entityVO.getRegionCode());
        codeList.add(entityVO.getStreetCode());
        List<AreaDTO> areaDtos = Optional.ofNullable(globalDictFeignClient.queryNames(codeList)).map(ResponseResult::getData).orElseGet(() -> Collections.EMPTY_LIST);
        entityVO.setAreas(JsonUtils.jsonToList(areaDtos, AreaVO.class));
        return entityVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        return employeeAddressMapper.deleteById(id) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modifyOldDefaultYesToNo(Long employeeId) {
        if (employeeId == null) {
            throw BusinessException.create("修改默认收货地址失败(会员id不能为空)!");
        }
        //把会员的老的默认地址改为非默认(默认地址只能有一个)
        LambdaUpdateWrapper<EmployeeAddressDO> updateWrapper = new LambdaUpdateWrapper<EmployeeAddressDO>().eq(EmployeeAddressDO::getEmployeeId, employeeId).eq(EmployeeAddressDO::getHasDefault, DefaultEnum.YES.getValue());
        EmployeeAddressDO up = new EmployeeAddressDO();
        up.setHasDefault(DefaultEnum.NO.getValue());
        up.setModifyTime(LocalDateTime.now());
        return employeeAddressMapper.update(up, updateWrapper) > 0;
    }


    private static EmployeeAddressServiceImpl getBean() {
        return SpringBeanUtil.getBean(EmployeeAddressServiceImpl.class);
    }

}